<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Article;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use App\Articlecat;


class ArticleController extends Controller
{
    //

    public function getAdd(){
    	
        $cat=new Articlecat;
        $cats=$cat->get();
    	return view('admin.articleadd',['cats'=>$cats]);
    }

    public function postAdd(request $req){
    	
    	$news=new Article;
    	//dd($req->all());
    	$news->title=$req->title;
    	$news->style=$req->style;
        $news->cat_id=$req->cat_id;
    	$news->username=$req->username;
    	$news->keywords=$req->keywords;
    	$news->content=$req->content;
    	$news->inputtime=date('Y-m-d H:i:s',time());

        $files=$req->file('thumb');
        //dd($req);
        
        $filename=md5(time().rand(1000,9999)).'.'.$files->getClientOriginalExtension();
        $realPath="upload/news/";
        $byte=$files->move($realPath,$filename);
    	$news->thumb=$realPath.$filename;
    	return $news->save()?'提交成功':'提交失败';
    
    	
    }

    public function articlelist(){


        $art=new Article;

        $arts=$art->paginate(3);
        //$art->get();


    	return view('admin.articlelist',['arts'=>$arts]);
    }
    public function articleedit(request $req,$nid){
        $arts=Article::find($nid);
        if(empty($_POST)){
          
        
        return view('admin.articleedit',['arts'=>$arts]);  
    }else{

        $arts->title=$req->title;
        $arts->style=$req->style;
        $arts->username=$req->username;
        $arts->keywords=$req->keywords;
        $arts->content=$req->content;

        $files=$req->file('thumb');
        $filename=md5(time().rand(1000,9999)).'.'.$files->getClientOriginalExtension();
        $realPath="upload/news/";
        $byte=$files->move($realPath,$filename);
        $arts->thumb=$realPath.$filename;
        
        $arts->updatetime=date('Y-m-d H:i:s');
       return $arts->save()?redirect('amarlist'):'修改失败';

    }
        
    }

    public function articledel($nid){
            return Article::where('new_id',$nid)->delete()?redirect('amarlist'):'删除失败';
    }


}
